package com.gr.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/getCheckCode")
public class GetCheckCodeController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //根据胡图工具类提供的工具生成验证码
        LineCaptcha captcha= CaptchaUtil.createLineCaptcha(200,100);
        //根据验证码对象获取对应的文本
        String checkCode=captcha.getCode();
        System.out.println(checkCode);
        //将验证文本存放到session中
        HttpSession session=req.getSession();
        session.setAttribute("checkCode",checkCode);
        ServletOutputStream outputStream=resp.getOutputStream();
        captcha.write(outputStream);
    }
}
